package com.fc.h5dispaly.h5display.util;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.util.Log;

/**
 * Created by fc on 2020/5/14
 * Describe:
 */

public class LogMonitor {
    private static LogMonitor mInstance = new LogMonitor();
    private HandlerThread mLogThread = new HandlerThread("fcLog");
    private Handler mLogHandler;
    private static final long TIME_BLOCK = 200L;

    private LogMonitor() {
        mLogThread.start();
        mLogHandler = new Handler(mLogThread.getLooper());
    }

    public static LogMonitor getInstance() {
        return mInstance;
    }


    public void startMonitor() {
        mLogHandler.postDelayed(mLogRunnable, TIME_BLOCK);
    }

    public void removeMonitor() {
        mLogHandler.removeCallbacks(mLogRunnable);
    }

    private static Runnable mLogRunnable = new Runnable() {
        @Override
        public void run() {
            StringBuilder sb = new StringBuilder();
            StackTraceElement[] stackTraceElements = Looper.getMainLooper().getThread().getStackTrace();
            for (StackTraceElement s : stackTraceElements) {
                sb.append(s.toString()).append("\n");
            }
            Log.e("LogMonitor", sb.toString());
        }
    };

}